#include<stdio.h>
#include<math.h>
int main()
{
	int n,i,j,x;
	scanf("%d",&n);
	int A,B;
	for(i = 0;i<n;i++)
	{
		x = 1; 
		scanf("%d %d",&A,&B);
	    for(j=0;j<B;j++)
	    {
	    	x = x*A;
	    	x = x%1000;
	   	}
		printf("%d\n",x);
	
	}
	return 0;
}
